﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransCode.Entitys;
using TransCode.Logic;

namespace TransCodeTask
{
    public class FeatureTask
    {
        private string processName = ConfigValue.GetConfigByName("FeaturePath");

        private string featureArguments = ConfigValue.GetConfigByName("FeatureArguments");

        private bool result = false;

        private ExecCmd cmd = null;

        //E:\zhangexe\campus\campus.avi.xml
        public bool TaskRunning(string fileName, string SourceFilePath)
        {
            result = false;
            string dir = fileName.Substring(0, fileName.IndexOf('.'));

            cmd = new ExecCmd();
            cmd.processName = processName;
            cmd.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(cmd_ErrorDataReceived);
            cmd.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(cmd_OutputDataReceived);
            cmd.Exec(string.Format(featureArguments, SourceFilePath + dir + ".avi.xml"));
            return result;
        }

        public void Dispose()
        {
            cmd.Dispose();
        }

        void cmd_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {
            if (e == null || e.Data == null)
            {
                return;
            }

            if (e.Data.Contains("end"))
            {               
                result = true;
                return;
            }

            if (e.Data.Length > 0)
            {
                string[] datas = e.Data.Split(new char[] { ' ' });

                double progress = 0;
                if (double.TryParse(datas[0].Remove(datas[0].Length - 1), out progress))
                {
                    
                }
            }
        }

        void cmd_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
        {

        }
    }
}
